package com.sanjiahantang.youjiaclient.module.me.contract.models.impl;

import android.content.Context;
import android.os.Handler;


import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.me.contract.beans.ContractBean;
import com.sanjiahantang.youjiaclient.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiaclient.module.me.contract.views.ContractView;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramContractBean;
import com.sanjiahantang.youjiaclient.utils.SPUtils;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zy on 2017/9/8.
 */

public class ContractModel implements IContractModel,StaticValue {
    @Override
    public void loadContractList(final Context context, final Map<String,String> param, final CallBack<ContractBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/project/contract/phone/searchProjectContractForCustomer", param,
                new BaseDelegate.ResultCallback<ContractBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(ContractBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void saveContractForPhoneDesign(final Context context, Map<String,String> param, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/project/contract/phone/saveContractForPhoneDesign", param,
                new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void findUserRequireByIdPhone(final Context context, Map<String, String> param, final CallBack<RequireListBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/require/findUserRequireByIdPhone",
                param, new BaseDelegate.ResultCallback<RequireListBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(RequireListBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void indexContractInfo(final Context context, Map<String, String> param, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/project/contract/phone/indexContractInfo",
                param, new BaseDelegate.ResultCallback<ProgramContractBean.Data>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(ProgramContractBean.Data response, Object tag) {
                        if (response != null && response.getContractCode() != null){
                            final ProgramContractBean.Data data = response;
                            Map<String,Object> param = new HashMap<>();
                            param.put("stateId","1");
                            param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
                            param.put("contractCode",response.getContractCode());
                            param.put("customerPhone", SPUtils.get(context, SPConfig.PHONE,"").toString());
                            param.put("signer2",response.getSigner1());
                            createTemplatePdfForm(context, param, new CallBack<BaseBean>() {
                                @Override
                                public void onSuccess(BaseBean response, Context context, Object tag) {
                                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                                        callBack.onSuccess(response,context,data);
                                    }else{
                                        callBack.onSuccess(response,context,tag);
                                    }
                                }
                                @Override
                                public void onFailure(String message) {
                                    callBack.onFailure(message);
                                }
                            });
                        }else{
                            callBack.onSuccess(null,context,tag);
                        }
                    }
                });
    }

    @Override
    public void saveContractForPhoneProject(final Context context, Map<String, Object> param, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/project/contract/phone/saveContractForPhoneProject",
                param, new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    private void createTemplatePdfForm(final Context context, Map<String,Object> param, final CallBack<BaseBean> callBack){
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/sign/createTemplatePdfForm",
                param, new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }
}
